package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.DatadictMapper;
import com.indusfo.spc.pojo.Datadict;
import com.indusfo.spc.service.DatadictService;
import com.indusfo.spc.vo.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 查询数据字典接口业务层实现类
 * @author xiet
 * 2018年11月22日 下午3:57:35
 */
@Service
public class DatadictServiceImpl implements DatadictService {

    private  static  final Logger logger = LoggerFactory.getLogger(DatadictServiceImpl.class);
    @Resource
    private DatadictMapper datadictMapper;

    /**
     * 查询数据字典
     */
    @Override
    public JSONObject queryDict(Integer dictId) {
        List<Datadict> lisDatadict = null;
        try {
            //判断传入参数是否为空
            if(dictId == null) {
                //参数如果为空抛出异常
                throw new ParamsErrorException("数据字典id不能为空");
            }
            //用list集合接收查询出来的结果集
            lisDatadict = datadictMapper.queryDict(dictId);
            //判断查询出来的结果集是否为空
            if(lisDatadict.isEmpty()) {
                //结果集为空
                return JSONObject.build(200, "没有查到相关数据！", lisDatadict);
            }
            return JSONObject.build(200, "查询成功！", lisDatadict);
        } catch (GlobalException e) {
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 查询数据字典 左部去重
     * @param datadict
     */
    @Override
    public JSONObject queryDictLeft(Datadict datadict) {
        List<Datadict> lisDatadict = null;
        try {
            Integer pagesize = datadict.getPagesize();
            Integer pageindex = datadict.getPageindex();
            if(pagesize != null && pageindex != null) {
                datadict.setIncept(pagesize*(pageindex - 1));
            }
            //用list集合接收查询出来的结果集
            lisDatadict = datadictMapper.queryDictLeft(datadict);
            //判断查询出来的结果集是否为空
            if(lisDatadict.isEmpty()) {
                //结果集为空
                return JSONObject.build(200, "没有查到相关数据！", lisDatadict);
            }
            int count = datadictMapper.queryDictLeftCount(datadict);
            return JSONObject.oK("查询成功", lisDatadict, count);
        } catch (GlobalException e) {
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 新增数据字典 左
     * @param datadict
     */
    @Override
    public JSONObject insertDictLeft(Datadict datadict) {
        datadict.setLevel("2");
        datadict.setItemId(1);
        try {
            checkParam1(datadict);
            // 字典编号不能重复
            Integer depCounts = datadictMapper.selecDatadictLeft(datadict);
            if (depCounts !=0) {
                throw new ParamsErrorException("该字典编号已存在");
            }
            int	row = datadictMapper.insertDictRight(datadict);
            if(row == 0) {
                throw new ModifyFailedException("数据新增失败！");
            }
            return JSONObject.oK("新增成功！");
        } catch (GlobalException e) {  //这里只捕获自定义异常
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    /**
     * 修改数据字典 左
     * @param datadict
     */
    @Override
    public JSONObject updateDictLeft(Datadict datadict) {
        try {
            checkParam1(datadict);
            int	row = datadictMapper.updateDictLeft(datadict);
            if(row == 0) {
                throw new ModifyFailedException("数据修改失败！");
            }
            return JSONObject.oK("修改成功！");
        } catch (GlobalException e) {  //这里只捕获自定义异常
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    /**
     * 删除数据字典 左部
     * @param lDictId
     * @return
     */
    @Override
    public JSONObject deleteDictLeft(Integer lDictId) {
        try {
            int judge = datadictMapper.queryDictCLvl(lDictId);
            if(judge>0){
                throw new ModifyFailedException("存在系统标准,无法删除！");
            }
            int	row = datadictMapper.deleteDictLeft(lDictId);
            if(row == 0) {
                throw new ModifyFailedException("数据删除失败！");
            }
            return JSONObject.oK("删除成功！");
        } catch (GlobalException e) {  //这里只捕获自定义异常
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }




    /**
     * 查询数据字典 右部
     * @param datadict
     */
    @Override
    public JSONObject queryDictRight(Datadict datadict) {
        List<Datadict> lisDatadict = null;
        List<Datadict> Datadicts = null;
        Integer dictId = datadict.getDictId();
        try {
            Integer pagesize = datadict.getPagesize();
            Integer pageindex = datadict.getPageindex();
            if(pagesize != null && pageindex != null) {
                datadict.setIncept(pagesize*(pageindex - 1));
            }
            if(dictId==null){
                Datadicts = datadictMapper.queryDictLeft(null);
                datadict.setDictId(Datadicts.get(0).getDictId());
            }
            //用list集合接收查询出来的结果集
            lisDatadict = datadictMapper.queryDictRight(datadict);
            //判断查询出来的结果集是否为空
            if(lisDatadict.isEmpty()) {
                //结果集为空
                Datadicts = datadictMapper.queryDictLeft(null);
                datadict.setDictId(Datadicts.get(0).getDictId());
                lisDatadict = datadictMapper.queryDictRight(datadict);
                if(lisDatadict.isEmpty()) {
                    //结果集为空
                    return JSONObject.build(200, "没有查到相关数据！", lisDatadict);
                }
            }
            int count = datadictMapper.queryDictRightCount(datadict);
            return JSONObject.oK("查询成功", lisDatadict, count);
        } catch (GlobalException e) {
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 新增数据字典 右部
     * @param datadict
     */
    @Override
    public JSONObject insertDictRight(Datadict datadict) {
        try {
            if(datadict.getDictId()==null){
                List<Datadict> Datadicts = datadictMapper.queryDictLeft(null);
                datadict.setDictId(Datadicts.get(0).getDictId());
                datadict.setDictName(Datadicts.get(0).getDictName());
            }
            checkParam(datadict);
            // 值域代码不能重复
            Integer depCounts = datadictMapper.selecDatadictRight(datadict);
            if (depCounts !=0) {
                throw new ParamsErrorException("该值域代码已存在");
            }
            int	row = datadictMapper.insertDictRight(datadict);
            if(row == 0) {
                throw new ModifyFailedException("数据新增失败！");
            }
            return JSONObject.oK("新增成功！");
        } catch (GlobalException e) {  //这里只捕获自定义异常
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    /**
     * 修改数据字典 右部
     * @param datadict
     */
    @Override
    public JSONObject updateDictRight(Datadict datadict) {
        try {
            if(datadict.getDictId()==null){
                List<Datadict> Datadicts = datadictMapper.queryDictLeft(null);
                datadict.setDictId(Datadicts.get(0).getDictId());
                datadict.setDictName(Datadicts.get(0).getDictName());
            }
            checkParam(datadict);
            int	row = datadictMapper.updateDictRight(datadict);
            if(row == 0) {
                throw new ModifyFailedException("数据修改失败！");
            }
            return JSONObject.oK("修改成功！");
        } catch (GlobalException e) {  //这里只捕获自定义异常
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    /**
     * 删除数据字典 右部
     * @param lDictId
     * @return
     */
    @Override
    public JSONObject deleteDictRight(Integer lDictId, Long[] lItemIds, String[] cLvls) {
        try {
            for(String cLvl : cLvls){
                if("1".equals(cLvl)){
                    throw new ModifyFailedException("存在系统标准,无法删除！");
                }
            }
            if(lDictId==null){
                List<Datadict> Datadicts = datadictMapper.queryDictLeft(null);
                lDictId = Datadicts.get(0).getDictId();
            }
            int	row = datadictMapper.deleteDictRight(lDictId,lItemIds);
            if(row == 0) {
                throw new ModifyFailedException("数据删除失败！");
            }
            return JSONObject.oK("删除成功！");
        } catch (GlobalException e) {  //这里只捕获自定义异常
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    /**
     * 参数校验 右
     *
     * @param datadict
     */
    private void checkParam(Datadict datadict) {
        if (datadict.getDictId() == null) {
            throw new ParamsErrorException("字典编号不能为空");
        }
        if (datadict.getDictId() >2147483647) {
            throw new ParamsErrorException("字典编号过大");
        }
        if (StringUtils.isEmpty(datadict.getDictName())) {
            throw new ParamsErrorException("请填写字典名称");
        }
        if (datadict.getDictName().getBytes().length > 50) {
            throw new ParamsErrorException("字典名称超过规定,长度50");
        }
        if (datadict.getItemId() == null) {
            throw new ParamsErrorException("值域编号不能为空");
        }
        if (datadict.getItemId() >2147483647) {
            throw new ParamsErrorException("值域编号过大");
        }
        if (StringUtils.isEmpty(datadict.getItemName())) {
            throw new ParamsErrorException("请填写值域名称");
        }
        if (datadict.getItemName().getBytes().length > 50) {
            throw new ParamsErrorException("值域名称超过规定,长度50");
        }
        if (datadict.getItemNo() == null) {
            throw new ParamsErrorException("值域代码不能为空");
        }
        if (StringUtils.isEmpty(datadict.getLevel())) {
            throw new ParamsErrorException("级别不能为空");
        }
        if(!"1".equals(datadict.getLevel())  &&  !"2".equals(datadict.getLevel())) {
            throw new ParamsErrorException("数据状态错误");
        }
    }

    /**
     * 参数校验 左
     *
     * @param datadict
     */
    private void checkParam1(Datadict datadict) {
        if (datadict.getDictId() == null) {
            throw new ParamsErrorException("字典编号不能为空");
        }
        if (datadict.getDictId() >2147483647) {
            throw new ParamsErrorException("字典编号过大");
        }
        if (StringUtils.isEmpty(datadict.getDictName())) {
            throw new ParamsErrorException("请填写字典名称");
        }
        if (datadict.getDictName().getBytes().length > 50) {
            throw new ParamsErrorException("字典名称超过规定,长度50");
        }
    }
}
